package com.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gp.dao.MaintainRecordDao;
import com.gp.entity.MaintainRecord;
import com.gp.service.MaintainRecordService;
import com.gp.util.CommonResult;
import com.gp.vo.MaintainRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/*
 * @program:carrent-pro
 * @author: luzzh
 * @Time: 2022-03-10 10:52
 * @Version: 1.0
 */
@Service
public class MaintainRecordServiceImpl implements MaintainRecordService {
    @Autowired
    private MaintainRecordDao maintainRecordDao;
    @Override
    public CommonResult findByPage(Integer currentPage, Integer pageSize, MaintainRecordVo maintainRecordVo) {
        QueryWrapper<MaintainRecord> queryWrapper = new QueryWrapper<>();
        if (maintainRecordVo.getMaintainCarName()!=""&&maintainRecordVo.getMaintainCarName()!=null) {
            queryWrapper.like("carName", maintainRecordVo.getMaintainCarName());
        }
        if (maintainRecordVo.getMaintainName()!=""&&maintainRecordVo.getMaintainName()!=null) {
            queryWrapper.like("name", maintainRecordVo.getMaintainName());
        }
        IPage<MaintainRecord> page = new Page<>(currentPage, pageSize);
        IPage<MaintainRecord> page1 = maintainRecordDao.findByPage(page, queryWrapper);
        System.out.println(page1 + "------------------");
        if (page1 != null) {
            return new CommonResult(200, "查询成功", page1);
        } else {
            return new CommonResult(500, "查询失败", page1);
        }
    }

    @Override
    public CommonResult updateRecord(@RequestBody MaintainRecord maintainRecord) {
        System.out.println(maintainRecord.toString());
        return new CommonResult(200,"修改成功",maintainRecordDao.updateById(maintainRecord));
    }

    @Override
    public CommonResult deleteById(Integer id) {
        int i = 0;
        i = maintainRecordDao.deleteById(id);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }
}
